package cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdx;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdx.vo.DpItemDxPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdx.vo.DpItemDxRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.dpitem.dpitemdx.vo.DpItemDxSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemDxDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemDxService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - ERP东线口岸制单货号详情")
@RestController
@RequestMapping("/erp/dp-item-dx")
@Validated
public class DpItemDxController {

    @Resource
    private DpItemDxService dpItemDxService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP东线口岸制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:create')")
    public CommonResult<Long> createDpItemDx(@Valid @RequestBody DpItemDxSaveReqVO createReqVO) {
        return success(dpItemDxService.createDpItemDx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP东线口岸制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:update')")
    public CommonResult<Boolean> updateDpItemDx(@Valid @RequestBody DpItemDxSaveReqVO updateReqVO) {
        dpItemDxService.updateDpItemDx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP东线口岸制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:delete')")
    public CommonResult<Boolean> deleteDpItemDx(@RequestParam("id") Long id) {
        dpItemDxService.deleteDpItemDx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP东线口岸制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:query')")
    public CommonResult<DpItemDxRespVO> getDpItemDx(@RequestParam("id") Long id) {
        DpItemDxDO dpItemDx = dpItemDxService.getDpItemDx(id);
        return success(BeanUtils.toBean(dpItemDx, DpItemDxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP东线口岸制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:query')")
    public CommonResult<PageResult<DpItemDxRespVO>> getDpItemDxPage(@Valid DpItemDxPageReqVO pageReqVO) {
        PageResult<DpItemDxDO> pageResult = dpItemDxService.getDpItemDxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemDxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP东线口岸制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item-dx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemDxExcel(@Valid DpItemDxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemDxDO> list = dpItemDxService.getDpItemDxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP东线口岸制单货号详情.xls", "数据", DpItemDxRespVO.class,
                        BeanUtils.toBean(list, DpItemDxRespVO.class));
    }

}